<?php
require '../system/configs/config.php';
require DIR_CONFIG . 'init.php';

// User
$user = new user($registry);
$registry->set('user', $user);

if (! $user->is_loaded())
	front::createInstance()->dispatch('adlogin');
else{
	// Check user is admin
	if (! $user->is_admin($user->userData[0]['u_admin'])){
		$user->logout('http://' . $_SERVER['HTTP_HOST'] . '/errors/deny.html');
		exit();
	}

	///////////////////////////////////LANGUAGE///////////////////////////////////////////
	$codeLang	= array();
	$titleLang	= array();
	$menuLang	= array();

	$langTotal	= $language->total();
	$lang_admin	= $language->langList(1);
	$lang_array = $language->langList();

	foreach ($lang_array as $row){
		$codeLang[]  = $row['lg_code'];
		$titleLang[] = $row['lg_name'];
	}

	$menuLang = array_combine($titleLang, $codeLang);

	$langs = $language->admin_lang($user->userData[0]['u_username'], $user->userData[0]['u_lang']);

	if (isset($_SESSION['codeLang'] ))
		$LSess = $_SESSION['codeLang'];
	else
		$LSess = DEFAULT_LANG;

	// Set session
	if (array_search($LSess, $langs) !== FALSE)
		$_SESSION['codeLang'] = $LSess;
	else
		$_SESSION['codeLang'] = $user->langCode;

	$langCode = $_SESSION['codeLang'];

	////////////////////////////PERMISSION/////////////////////////////
	// We define roles
	$guest = permr::READ;
	$editor = $guest | permr::EDIT;
	$moderator = $editor | permr::PUBLISH;
	$publisher = $moderator & ~permr::EDIT;
	$admin = $moderator | permr::DELETE;

	$perms = array(
			'read'=>permr::READ,
			'edit'=>permr::EDIT,
			'publish'=>permr::PUBLISH,
			'delete'=>permr::DELETE);

	$userRight = permr::userGroup($perms, $user->userData[0]['u_rights']);

	/*-------------------------Modules access------------------------*/
	$modules = array(
			'config',
			'users',
			'category',
			'ads',
			'news',
			'products',
			'services',
			'static',
			'system');

	$mLabel = array(
			$language->get('m_config'),
			$language->get('m_users'),
			$language->get('m_category'),
			$language->get('m_ads'),
			$language->get('m_news'),
			$language->get('m_product'),
			$language->get('m_services'),
			$language->get('m_static'),
			$language->get('m_system')
			);

	$userMods = permm::checkMod($user->userData[0]['u_rights'], $user->userID, $modules);

	////////////////////////////MENU/////////////////////////////

	front::createInstance()->dispatch(ADMIN);
}